<!--
 * @Author: wangding
 * @Date: 2020-11-11 15:58:31
 * @LastEditTime: 2020-12-25 17:39:35
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \solaxcloud\src\views\device\sale\add.vue
-->
<template>
    <div class="buyPage-main-box-add">
        <el-dialog
                :title="$t('devUpgrade.VersionToUp')"
        :visible="newVisible"
        width="35%"
        @close="()=>{$store.commit('setNewVisible',false)}"
        :close-on-click-modal='false'
        >   
            <el-row :gutter="20">
                <el-col :span="19" :offset="2">
                    <el-form ref="form" :model="form" label-width="120px" >
                        <el-form-item :label="$t('version.versionAdd.versionNum')" prop="wifiSns" :rules="[{ required: true, message: $t('index_js_notNull')}]">
                            <el-select v-model="form.wifiSns" style="width:100%" filterable :placeholder="$t('pleaseSelect')">
                                <el-option
                                v-for="item in addValueList"
                                :key="item.jsonString"
                                :label="item.longVersion"
                                :value="item.jsonString">
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-form>
                </el-col>
            </el-row>
            <span slot="footer" class="dialog-footer">
                <el-button @click="()=>{$store.commit('setNewVisible',false)}">{{$t('cancel')}}</el-button>
                <el-button type="primary" @click="save('form')">{{$t('sure')}}</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import baseFile from '../../../components/common/baseFileInput'
export default {
    components: {
        baseFile
    },
    props: {
        addValueList:{
            type:Array,
            default:()=>{return []}
        },
        selectDataList:{
            type:Array,
            default:()=>{return []}
        },
        inverterType:{
            type:String,
            default:''
        },
        scope:{
            type:String,
            default:''
        },
    },
    data() {
        return {
            form:{
                wifiSns:""
            },
            cloudType:[{
                id:'0',
                text:'Solax Cloud'
            },{
                id:'1',
                text:'QCELLS'
            }]
        };
    },
    computed: {
        newVisible(){
            return this.$store.state.sysStore.newVisible
        }
    },
    watch: {

    },
    created() {

    },
    mounted() {

    },
    methods: {
        async updateUpdateVersion(){
            let selectRowsV = this.selectDataList.map(item =>{return item.inverterVersion})
            let eqTypeValue = this.inverterType
            let scopeValue = this.scope
            let selectV = ''
            let flagMsg = false
            this.addValueList.forEach(item => {
                if(item.jsonString == this.form.wifiSns){
                    selectV = parseInt(item.version)
                }
            })
            if(eqTypeValue == 3 && scopeValue == 4){
                for(let i=0;i<selectRowsV.length;i++){
                    let oldV = parseInt(selectRowsV[i])
                    let newV = parseInt(selectV)
                    if(oldV == 1 || oldV == 3){
                        if(!(newV == 1 || newV == 3)){
                            flagMsg = true
                        }
                    }
                    if(oldV == 2){
                        if(newV == 1 || newV == 3){
                            flagMsg = true
                        }
                    }
                   
                }
            }
            if(flagMsg){
                this.comm.showMsg(false, this.$t('wifi.arrUploadMsg'))
                return
            }
            let taskId = JSON.parse(this.form.wifiSns).taskId
            let snStr = ''
            let wifisnArr = []
            this.selectDataList.forEach(item =>{
                wifisnArr.push(taskId+"#"+item.wifiSN)
            })
            wifisnArr = wifisnArr.join(';')
            let res = await this.API.updateUpdateVersion({
                wifiSns:wifisnArr,
                inverterType:this.inverterType,
                scope:this.scope
            })
            // if(this.comm.showMsg(res.success, this.$t('devUpgrade.waitForUpgrade'))){
            //     this.$emit('uploadData')
            //     this.$store.commit('setNewVisible',false)
            // }
            if(res.success){
                this.comm.showMsg(true, this.$t('devUpgrade.waitForUpgrade'))
                this.$emit('uploadData')
                this.$store.commit('setNewVisible',false)
            }else{
                this.comm.showMsg(false)
            }
        },
        save(formName){
            this.$refs[formName].validate((valid) => {
              if (valid) {
                  this.updateUpdateVersion()
              } else {
                console.log('error submit!!');
                return false;
              }
            });
        },
    },
};
</script>

<style  lang="less" >
.buyPage-main-box-add{
    .el-form-item__content{
        display: flex;
    }
    .icon-fontsize{
        font-size: 25px;
        margin-left: 10px;
        display: block;
        width: 25px;
        height: 33px;
    }
}
</style>
